package com.kitty.game.equip.handler;

import com.kitty.game.config.Equip;
import com.kitty.game.equip.EquipDataPool;
import com.kitty.game.equip.model.RoleEquip;
import com.kitty.game.role.model.Role;
import com.kitty.game.utils.AsktaoUtil;
import com.kitty.game.utils.Const;
import com.kitty.game.utils.NotifyModules;
import com.kitty.listener.EventDispatcher;
import com.kitty.listener.EventType;
import com.kitty.listener.event.ChangeEquipEvent;
import com.kitty.listener.event.EquipUpgradeLevelChangeEvent;
import com.kitty.common.model.Reason;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;

import static com.kitty.game.utils.Const.*;

@Component("EquipInheritHandler")
@Slf4j
public class EquipInheritHandler extends UpgradeHandler {
    private static final String ERROR_ITEM = "请放入正确的装备。";
    private static final String SUCCESS_MESSAGE = "恭喜，你花费{0}文钱、#R{1}#n元宝，#R{2}#n成功继承了#R{3}#n的改造等级。";

    /**
     * 开始打造 返回打造成功或者失败
     */
    @Override
    public String doUpgrade(Role role, Object... params) {
        return null;
    }

    /**
     * 入口
     */
    @Override
    public String acceptUpgrade(Role role, int upgradePos, String param) {
        String[] strings = param.split("\\|");
        int oEquipPos = Integer.parseInt(strings[0]);
        boolean isUseSilver = strings[1].equals("0");
        RoleEquip mEquip = equipService.getRoleEquipByPos(role, upgradePos);
        RoleEquip oEquip = equipService.getRoleEquipByPos(role, oEquipPos);
        if (mEquip == null || oEquip == null) {
            return ERROR_ITEM;
        }
        Equip mEquipSet = EquipDataPool.getByName(mEquip.getName());
        Equip oEquipSet = EquipDataPool.getByName(oEquip.getName());
        if (mEquipSet == null || oEquipSet == null) {
            log.error("继承装备未找到配置=={}", mEquip.getName());
            return ERROR_ITEM;
        }
        if (role.getLevel() < 70) {
            return MessageFormat.format(ROLE_RESOURCE_NOT_ENOUGH, "等级");
        }
        /**如果主装备的改造等级大于5或者副装备的等级小于等于主装备那么就返回*/
        if (mEquip.queryUpgradeLevel() >= 5 || oEquip.queryUpgradeLevel() <= mEquip.queryUpgradeLevel() || oEquip.queryUpgradeLevel() < 5) {
            return ERROR_ITEM;
        }
        /**如果要继承的装备跟副装备不是同一个类型的返回*/
        if (mEquip.queryEquipPos(false) != oEquip.queryEquipPos(false)) {
            return ERROR_ITEM;
        }
        if (mEquip.isEverLimit() || oEquip.isEverLimit()) {
            return ERROR_ITEM;
        }
        if (!mEquip.queryColor().equals("绿色")) {
            return "蓝属性满3条的绿装备才可以作为主装备进行改造继承。";
        }
        int coin = 3000000;// getCostCoin(mEquip, oEquip);
        if (oEquipSet.getPosition() == Const.POS_WEAPON){
            if (oEquip.queryUpgradeLevel() == 12){
                coin = 18000000;
            }else if (oEquip.queryUpgradeLevel() == 11){
                coin = 15000000;
            }else if (oEquip.queryUpgradeLevel() == 10){
                coin = 12000000;
            }else if (oEquip.queryUpgradeLevel() == 9){
                coin = 10000000;
            }else if (oEquip.queryUpgradeLevel() == 8){
                coin = 8000000;
            }else if (oEquip.queryUpgradeLevel() == 7){
                coin = 6000000;
            }
        }else {
            if (oEquip.queryUpgradeLevel() == 12){
                coin = 15000000;
            }else if (oEquip.queryUpgradeLevel() == 11){
                coin = 12000000;
            }else if (oEquip.queryUpgradeLevel() == 10){
                coin = 10000000;
            }else if (oEquip.queryUpgradeLevel() == 9){
                coin = 8000000;
            }else if (oEquip.queryUpgradeLevel() == 8){
                coin = 6000000;
            }else if (oEquip.queryUpgradeLevel() == 7){
                coin = 4000000;
            }
        }
        coin = coin/30;


        // 装备继承消耗
        //coin = 500000;
        // int money = getCostMoney(mEquip, oEquip);
        int money = getCostCoin(mEquip, oEquip);
        log.error("继承消耗金钱[{}],积分[{}]", money, coin);
        /**如果优先使用银元宝 那么先把银元宝取出来判断 不够的话再取金元宝*/
        if (role.getMoney() < money) {
            return MessageFormat.format(ROLE_RESOURCE_NOT_ENOUGH, MONEY_NAME);
        }
        if (isUseSilver) {
            if (role.getSiver() < coin) {
                /**如果银元宝不足 但是加金元宝能购买*/
                if (role.getSiver() > 0 && (role.getGold() - role.getSiver()) >= coin) {
                    int needGold = coin - role.getSiver();
                    roleService.subtractSiver(role, role.getSiver(), Reason.EQUIP_INHERIT);
                    mEquip.addLimitTime(1);
                    roleService.subtractGold(role, needGold, Reason.EQUIP_INHERIT);
                } else if (role.getGold() < coin) {
                    return MessageFormat.format(ROLE_RESOURCE_NOT_ENOUGH, GOLD_NAME);
                } else {
                    /**金元宝足够*/
                    roleService.subtractGold(role, coin, Reason.EQUIP_INHERIT);
                }
            } else {
                mEquip.addLimitTime(1);
                roleService.subtractSiver(role, coin, Reason.EQUIP_INHERIT);
            }
        } else {
            if (role.getGold() >= coin) {
                roleService.subtractGold(role, coin, Reason.EQUIP_INHERIT);
            } else if (role.getGold() < coin) {
                return MessageFormat.format(ROLE_RESOURCE_NOT_ENOUGH, GOLD_NAME);
            }
        }


        roleService.subtractMoney(role, money);
        int oLevel = oEquip.queryUpgradeLevel();
        int oProgress = oEquip.queryUpgradeProgress();
        int oJindu = oEquip.getGaizaoJindu();

        int mLevel = mEquip.queryUpgradeLevel();
        int mProgress = mEquip.queryUpgradeProgress();
        int mJindu = mEquip.getGaizaoJindu();



        mEquip.alterUpgradeLevel(oLevel);
        mEquip.setGaizaoJindu(oJindu);
        mEquip.alterUpgradeProgress(oProgress);
        equipService.addUpgradeField(mEquipSet, mEquip);

        oEquip.alterUpgradeLevel(mLevel);
        oEquip.setGaizaoJindu(mJindu);
        oEquip.alterUpgradeProgress(mProgress);
        equipService.addUpgradeField(oEquipSet, oEquip);

        pushOperateResult(role, UPGRADE_SUCCESS);
        /**如果是银元宝操作 主副装备全部限制60天*/

        mEquip.addLimitTime(6);
        oEquip.addLimitTime(6);

        refreshRoleEquip(role, mEquip);
        refreshRoleEquip(role, oEquip);

        /**判断当前的改造成功是否需要发谣言*/
        if (oLevel > 5) {
            String msg;
            if (oLevel > 8) {
                msg = "惊闻玩家#Y%s#n成功将#R{%s}#n的改造等级提升到#R%d级#n了，得此神器，夫复何求啊！#49m";
            } else if (oLevel == 8) {
                msg = "#35m这难道就是传说中的神器吗？你们看，这就是#Y%s#n已经成功将#R{%s}#n改#R%d#n了......";
            } else if (oLevel == 7) {
                msg = "#63m天呐！#Y%s#n居然成功将#R{%s}#n的改造等级提升到#R%d级#n了！";
            } else {
                msg = "#41m快来看啊！#Y%s#n成功将#R{%s}#n的改造等级提升到#R%d级#n了。";
            }
            equipService.sendEquipMsg(role, mEquip, oLevel, msg);
        }
        if (MAIN_PAGE.contains((byte) oEquipPos)) {
            /**产生一个装备改变事件*/
            EventDispatcher.getInstance().fireEvent(new ChangeEquipEvent(EventType.CHANGE_EQUIP, role, null));
        }
        /**产生一个装备改造等级改变事件*/
        EventDispatcher.getInstance().fireEvent(new EquipUpgradeLevelChangeEvent(EventType.EQUIP_UPGRADE_LEVEL_CHANGE, role, mEquip, (short) mLevel, mEquip.queryUpgradeLevel()));
        EventDispatcher.getInstance().fireEvent(new EquipUpgradeLevelChangeEvent(EventType.EQUIP_UPGRADE_LEVEL_CHANGE, role, oEquip, (short) oLevel, oEquip.queryUpgradeLevel()));

        return MessageFormat.format(SUCCESS_MESSAGE, AsktaoUtil.getMoneyFormat(money), coin, mEquip.getName(), oEquip.getName());
    }

    /**
     * 检查需要的资源是否足够
     */
    @Override
    public String checkResource(Object... objects) {
        return null;
    }

    /**
     * 打造需要的金钱
     */
    @Override
    public int needCash(short level, int pos) {
        return 0;
    }

    @Override
    int getNotifyType() {
        return NotifyModules.NOTIFY_EQUIP_UPGRADE_INHERIT_OK;
    }

    /**
     * 取操作类型的字符串
     */
    @Override
    public String getUpdateTypeStr() {
        return "upgrade_inherit";
    }
}
